/*
* vuex 全站存储
* get: this.$store.state.模块.state
* 如： this.$store.state.app.language
* set: this.$store.dispatch(actions, value)
* 如： this.$store.dispatch('setLanguage', 'en');
*/

// 对旧账户处理
let scatterLocalStorage = localStorage.getItem('scatter');
if (scatterLocalStorage) {
  scatterLocalStorage = JSON.parse(scatterLocalStorage);
  if (!scatterLocalStorage.chain) {
    scatterLocalStorage.chain = 'eos';
  }
} else {
  scatterLocalStorage = {
    identity: null,
    wallet: '',
    chain: 'eos',
    online: false, // 账户是否在线
  }
}

// 对旧节点处理
let nodeCheckedStorage = localStorage.getItem('nodeChecked');
if (nodeCheckedStorage) {
  const oldnode = JSON.parse(nodeCheckedStorage);
  if (oldnode.bos) {
    nodeCheckedStorage = oldnode;
  } else {
    const nodeCheckeds = {
      eos: oldnode,
      bos: {
        url: 'https://api.bossweden.org',
        host: 'api.bossweden.org',
        protocol: 'https',
        port: '443',
        area: 'Japan', // 选中地区
        time: '55', // 选中时间
        tip: -1, // 选中延迟状态
      }
    }
    nodeCheckedStorage = nodeCheckeds;
  }
} else {
  nodeCheckedStorage = {
    eos: {
      url: 'https://api.eosbeijing.one',
      host: 'api.eosbeijing.one',
      protocol: 'https',
      port: '443',
      area: 'BeiJing', // 选中地区
      time: '55', // 选中时间
      tip: -1, // 选中延迟状态
    },
    bos: {
      url: 'https://api.bossweden.org',
      host: 'api.bossweden.org',
      protocol: 'https',
      port: '443',
      area: 'Japan', // 选中地区
      time: '55', // 选中时间
      tip: -1, // 选中延迟状态
    },
  }
}

const app = {
  state: {
    language: localStorage.getItem('language') || 'en', // 多语言
    theme: localStorage.getItem('theme') || 'theme-278EDA', // 主题 默认白天
    userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {}, // 用户信息存储
    trade: localStorage.getItem('trade') ? JSON.parse(localStorage.getItem('trade')) : { id: null },
    scatter: scatterLocalStorage,
    badge: localStorage.getItem('badge') ? JSON.parse(localStorage.getItem('badge')) : false, // 未读订单消息
    marketPrice: null, // 当前交易对的市场价
    exchangeAccount: {
      eos: null,
      bos: null,
    }, // 智能合约
    accountAgree: localStorage.getItem('accountAgree') ? JSON.parse(localStorage.getItem('accountAgree')) : false, // 已同意使用协议
    openShijia: [1, 1], // 是否开启市价买卖单（没有对手盘）['买入', '卖出'] 1是开启， 0是关闭
    accountInfo: localStorage.getItem('accountInfo') ? JSON.parse(localStorage.getItem('accountInfo')) : {}, // 存放账户account,token
    upgradeSystem: false, // 是否停止交易下单、撤单
    klineConfig: {
      interval: '240', // 分辨率，时间
      intervalRam: '60', // RAM分辨率，时间
      intervalRex: '60', // RAM分辨率，时间
      intervalCpu: '60', // RAM分辨率，时间
    },
    nodeChecked: nodeCheckedStorage, // 节点切换参数
    ownEntrustOrder: [], // 自己的委托订单字段存储
    accountRes: {}, // 账户余额等数据
    favoriteData: localStorage.getItem('favoriteData') ? JSON.parse(localStorage.getItem('favoriteData')) : [], // 自选区数据
    pricesCurrentcy: localStorage.getItem('pricesCurrentcy') ? JSON.parse(localStorage.getItem('pricesCurrentcy')) : {}, // 自选区数据
    notice: localStorage.getItem('notice') ? JSON.parse(localStorage.getItem('notice')) : [], // 公告数据
    codeList: localStorage.getItem('codeList') ? JSON.parse(localStorage.getItem('codeList')) : [], // 币种列表
    stableCode: localStorage.getItem('stableCode') ? JSON.parse(localStorage.getItem('stableCode')) : [], // 币种列表
    symbolRecommendList: localStorage.getItem('symbolRecommendList') ? JSON.parse(localStorage.getItem('symbolRecommendList')) : [], // 推荐交易对
    chargingStatus: {
      coinConvertStatus: 1, // 兑换交易服务器状态 0-维护 1-正常
      // withdrawStatus: 1, // 提币服务器状态 0-维护 1-正常
      // depositStatus: 1 // 充币服务器状态 0-维护 1-正常
    },
  },
  mutations: {
    SET_LANGUAGE: (state, language) => {
      state.language = language;
      localStorage.setItem('language', language);
    },
    SET_THEME: (state, theme) => {
      state.theme = theme;
      localStorage.setItem('theme', theme);
      // localStorage.removeItem('tradingview.chartproperties');
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    SET_TRADE: (state, trade) => {
      state.trade = trade;
      localStorage.setItem('trade', JSON.stringify(trade));
    },
    SET_SCATTER: (state, scatter) => {
      state.scatter = scatter;
      localStorage.setItem('scatter', JSON.stringify(scatter));
    },
    SET_BADGE: (state, badge) => {
      state.badge = badge;
      localStorage.setItem('badge', JSON.stringify(badge));
    },
    SET_MARKETPRICE: (state, marketPrice) => {
      state.marketPrice = marketPrice;
    },
    SET_EXCHANGEACCOUNT: (state, exchangeAccount) => {
      state.exchangeAccount = exchangeAccount;
      localStorage.setItem('exchangeAccount', JSON.stringify(exchangeAccount));
    },
    SET_ACCOUNTAGREE: (state, accountAgree) => {
      state.accountAgree = accountAgree;
      localStorage.setItem('accountAgree', JSON.stringify(accountAgree));
    },
    SET_OPENSHIJIA: (state, openShijia) => {
      state.openShijia = openShijia;
    },
    SET_ACCOUNTINFO: (state, accountInfo) => {
      state.accountInfo = accountInfo || {};
      localStorage.setItem('accountInfo', JSON.stringify(accountInfo));
    },
    SET_UPGRADESYSTEM: (state, upgradeSystem) => {
      state.upgradeSystem = upgradeSystem;
    },
    SET_NODECHECKED: (state, nodeChecked) => {
      state.nodeChecked = nodeChecked;
      localStorage.setItem('nodeChecked', JSON.stringify(nodeChecked));
    },
    SET_KLINECONFIG: (state, klineConfig) => {
      state.klineConfig = klineConfig;
    },
    SET_APPDOWNLOADHEIGHT: (state, appDownloadHeight) => {
      state.appDownloadHeight = appDownloadHeight;
    },
    SET_OWNENTRUSTORDER: (state, ownEntrustOrder) => {
      state.ownEntrustOrder = ownEntrustOrder;
    },
    SET_ACCOUNTRES: (state, accountRes) => {
      state.accountRes = accountRes;
    },
    SET_FAVORITEDATA: (state, favoriteData) => {
      state.favoriteData = favoriteData;
      localStorage.setItem('favoriteData', JSON.stringify(favoriteData));
    },
    SET_PRICESCURRENTCY: (state, pricesCurrentcy) => {
      state.pricesCurrentcy = pricesCurrentcy;
      localStorage.setItem('pricesCurrentcy', JSON.stringify(pricesCurrentcy));
    },
    SET_NOTICE: (state, notice) => {
      state.notice = notice;
      localStorage.setItem('notice', JSON.stringify(notice));
    },
    SET_CODELIST: (state, codeList) => {
      state.codeList = codeList;
      localStorage.setItem('codeList', JSON.stringify(codeList));
    },
    SET_STABLECODE: (state, stableCode) => {
      state.stableCode = stableCode;
      localStorage.setItem('stableCode', JSON.stringify(stableCode));
    },
    SET_SYMBOLRECOMMENDLIST: (state, symbolRecommendList) => {
      state.symbolRecommendList = symbolRecommendList;
      localStorage.setItem('symbolRecommendList', JSON.stringify(symbolRecommendList));
    },
    SET_COINCONVERTSTATUS: (state, chargingStatus) => {
      state.chargingStatus = chargingStatus;
      localStorage.setItem('chargingStatus', JSON.stringify(chargingStatus));
    },
  },
  actions: {
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language);
    },
    setTheme({ commit }, theme) {
      commit('SET_THEME', theme);
    },
    setUserInfo({ commit }, userInfo) {
      commit('SET_USERINFO', userInfo);
    },
    setTrade({ commit }, trade) {
      commit('SET_TRADE', trade);
    },
    setScatter({ commit }, scatter) {
      commit('SET_SCATTER', scatter);
    },
    setBadge({ commit }, badge) {
      commit('SET_BADGE', badge);
    },
    setMarketPrice({ commit }, marketPrice) {
      commit('SET_MARKETPRICE', marketPrice);
    },
    setExchangeAccount({ commit }, exchangeAccount) {
      commit('SET_EXCHANGEACCOUNT', exchangeAccount);
    },
    setAccountAgree({ commit }, accountAgree) {
      commit('SET_ACCOUNTAGREE', accountAgree);
    },
    setOpenShijia({ commit }, openShijia) {
      commit('SET_OPENSHIJIA', openShijia);
    },
    setAccountInfo({ commit }, accountInfo) {
      commit('SET_ACCOUNTINFO', accountInfo || {});
    },
    setUpgradeSystem({ commit }, upgradeSystem) {
      commit('SET_UPGRADESYSTEM', upgradeSystem);
    },
    setNodeChecked({ commit }, nodeChecked) {
      commit('SET_NODECHECKED', nodeChecked);
    },
    setKlineConfig({ commit }, klineConfig) {
      commit('SET_KLINECONFIG', klineConfig);
    },
    setAppDownloadHeight({ commit }, appDownloadHeight) {
      commit('SET_APPDOWNLOADHEIGHT', appDownloadHeight);
    },
    setOwnEntrustOrder({ commit }, ownEntrustOrder) {
      commit('SET_OWNENTRUSTORDER', ownEntrustOrder);
    },
    setAccountRes({ commit }, accountRes) {
      commit('SET_ACCOUNTRES', accountRes);
    },
    setFavoriteData({ commit }, favoriteData) {
      commit('SET_FAVORITEDATA', favoriteData);
    },
    setPricesCurrentcy({ commit }, pricesCurrentcy) {
      commit('SET_PRICESCURRENTCY', pricesCurrentcy);
    },
    setNotice({ commit }, notice) {
      commit('SET_NOTICE', notice);
    },
    setCodeList({ commit }, codeList) {
      commit('SET_CODELIST', codeList);
    },
    setStableCode({ commit }, stableCode) {
      commit('SET_STABLECODE', stableCode);
    },
    setSymbolRecommendList({ commit }, symbolRecommendList) {
      commit('SET_SYMBOLRECOMMENDLIST', symbolRecommendList);
    },
    setCoinConvertStatus({ commit }, chargingStatus) {
      commit('SET_COINCONVERTSTATUS', chargingStatus);
    },
  }
};

export default app;
